//
//  APImycityWeek.m
//  Bracelet
//
//  Created by jing zhao on 2/26/15.
//  Copyright (c) 2015 jing zhao. All rights reserved.
//

#import "APImycityWeek.h"
#import "APIConnection.h"

#import <AFNetworking/AFNetworking.h>

#import "FileReadAndWrite.h"
#define FILENAMECACHE_APIMYCITYWEEK @"fileName_apiMycityWeek"

@implementation APImycityWeek

+(void)getMyCityWeekDataSuccess:(void (^)(APImycityTodayTotal *mycity))successBlock
                        failure:(void (^)( NSError *error)) failureBlock
{
    NSString *strCityId = [[[NSUserDefaults standardUserDefaults]objectForKey:APIKEYUSERINFO]objectForKey:APIKEYCITYID];
    NSDictionary *data = @{@"flag":@"week",@"cityId":strCityId,@"ios_android_flag":@"ios"};
    
    [[APIConnection shareInstance]post:@"mycity/getTotalData_city" data:data netError:^(NSError *error) {
        failureBlock(error);
        
        //取缓存
        NSDictionary *dicCanch = [FileReadAndWrite getTheStorageDataWithFile:FILENAMECACHE_APIMYCITYWEEK];
        if (dicCanch) {
            NSDictionary *dicSuccess = [dicCanch objectForKey:APIKEYData];
            successBlock([APImycityTodayTotal apiTodayWithData:dicSuccess]);
        }
    } apiFinish:^(id data) {
        
        NSDictionary *dictionaryTemp = data;
        NSInteger codeTemp= [[dictionaryTemp objectForKey:APIKEYCODE]integerValue];
        if (codeTemp ==0) {
            NSDictionary *dicSuccess = [dictionaryTemp objectForKey:APIKEYData];
            successBlock([APImycityTodayTotal apiTodayWithData:dicSuccess]);
            
            //存入缓存
            [FileReadAndWrite storageDataWithFile:FILENAMECACHE_APIMYCITYWEEK andData:dictionaryTemp];

        }
        else
        {
            failureBlock(nil);
        }
    }];
}
@end
